﻿using System;
using System.Linq;
using System.Windows.Media;
using Loki.Commands;
using Loki.Utils;
using Luna.Model.Budget;
using Luna.UI.Resources;

namespace Luna.UI.Budget
{
    public class CategoryListViewModel : LunaViewModel
    {
        public LokiCollection<DisplayableCategory> Categories { get; private set; }

        public CategoryListViewModel()
        {
            Categories = CreateTrackedCollection<DisplayableCategory>();

            DisplayName = FormNames.CATEGORY_LIST;
            GroupName = FormNames.GROUP_BUDGET;

            Init += CategoryListViewModel_Init;
            Load += CategoryListViewModel_Load;
        }

        private void CategoryListViewModel_Load(object P_Sender, EventArgs e)
        {
            LoadCategories();
        }

        private void CategoryListViewModel_Init(object P_Sender, EventArgs e)
        {
            Commands.Handle(Loki.Commands.ApplicationCommands.SAVE, ApplicationSave_CanExecute, ApplicationSave_Execute);
        }

        private void LoadCategories()
        {
            Categories.BeginInit();
            Categories.Clear();

            foreach (var L_Source in ApplicationModel.Budget.Categories.OrderBy(x => x.Name).Select(x => x.EditableCopy))
            {
                var L_Display = new DisplayableCategory();
                L_Display.BeginInit();
                L_Display.Color = (Color)ColorConverter.ConvertFromString(L_Source.Color);
                L_Display.Description = L_Source.Description;
                L_Display.Name = L_Source.Name;
                L_Display.Category = L_Source;
                L_Display.EndInit();
                Categories.Add(L_Display);
            }

            Categories.EndInit();
            AcceptChanges();
        }

        private void ApplicationSave_CanExecute(object P_Sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute |= IsChanged;
        }

        private void ApplicationSave_Execute(object P_Sender, CommandEventArgs e)
        {
            Validate();
            if (IsValid)
            {
                foreach (var L_ModifiedCategory in Categories)
                {
                    IEditableCategory L_Category = L_ModifiedCategory.Category;
                    if (L_Category == null)
                    {
                        L_Category = ApplicationModel.Create<IEditableCategory>();
                        L_Category.ID = L_ModifiedCategory.ID;
                    }

                    L_Category.Name = L_ModifiedCategory.Name;
                    L_Category.Description = L_ModifiedCategory.Description;
                    L_Category.Color = L_ModifiedCategory.Color.ToString();

                    ApplicationModel.Budget.Save(L_Category);
                }

                foreach (var L_DeletedCategory in Categories.RemovedItems)
                {
                    if (L_DeletedCategory.Category != null)
                    {
                        ApplicationModel.Budget.Delete(L_DeletedCategory.Category);
                    }
                }

                LoadCategories();
            }
            else
            {
                UserWarning(Error);
            }
        }

        public override bool Validate(Loki.UI.ValidationMode P_Mode)
        {
            bool L_Buffer = base.Validate(P_Mode);
            foreach (var L_Category in Categories)
            {
                L_Buffer &= L_Category.Validate(P_Mode);
            }

            return L_Buffer;
        }
    }
}